﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Diagnostics;
using System.Windows.Forms;

namespace SmartRFIDSystem19.model
{
    class PortUtils
    {
        public static Byte[] getCardId()
        {
            Byte Address = 0x00;
            Byte ReadSN = 0x10;
            Byte[] send_packet = new Byte[100];
            int bcc_1 = 0;
            int idx = 0;
            send_packet[0] = 0xAB;
            send_packet[1] = 0xBA;
            send_packet[2] = Address;
            send_packet[3] = ReadSN;
            send_packet[4] = 0x00;
            for (idx = 2; idx <= 4; idx++)
            {
                bcc_1 ^= send_packet[idx];
            }
            send_packet[idx++] = Convert.ToByte(bcc_1);
            return send_packet;
        }
        public static Byte[] read(int a, int b, string key)
        {
            int i = 8;
            int bcc = 0;
            Byte[] send = new Byte[200];
            send[0] = 0xAB;
            send[1] = 0xBA;
            send[2] = 0x00;//读取指定扇区指定
            send[3] = 0x12;
            send[4] = 0x09;//指定数据长度
            send[5] = Convert.ToByte(a);//指定扇区
            send[6] = Convert.ToByte(b);//指定块区
            send[7] = 0x0B;//密钥B
            Byte[] m = strTobyte(key);
            for (int j = 0; j < 6; j++)
            {
                send[i] = m[j];//密钥数组
                i++;
            }
            for (i = 2; i <= 13; i++)
            {
                bcc ^= send[i];//校验
            }
            send[i++] = Convert.ToByte(bcc);
            return send;
        }
       
        public static Byte[] add(UInt32 money, string key)
        {
            int i = 8;
            int eh = 0;
            Byte[] send = new Byte[200];
            send[0] = 0xAB;
            send[1] = 0xBA;
            send[2] = 0x00;
            send[3] = 0x13;
            send[4] = 0x19;//长度
            send[5] = 0x00;
            send[6] = 0x01;
            send[7] = 0x0B;
            Byte[] m = strTobyte(key);
            for (int j = 0; j < 6; j++)
            {
                send[i] = m[j];
                i++;
            }
            for (i = 14; i <= 25; i++)
            {
                send[i] = 0x00;
            }
            send[26] = (Byte)(money >> 24);
            send[27] = (Byte)(money >> 16);
            send[28] = (Byte)(money >> 8);
            send[29] = (Byte)(money);
            for (i = 2; i < 30; i++)
            {
                eh ^= send[i];
            }
            send[i++] = Convert.ToByte(eh);
            return send;
        }
        public static Byte[] init(string key)
        {
            int i = 8;
            int eh = 0;
            Byte[] send = new Byte[200];
            send[0] = 0xAB;
            send[1] = 0xBA;
            send[2] = 0x00;
            send[3] = 0x13;
            send[4] = 0x19;//长度
            send[5] = 0x00;
            send[6] = 0x01;
            send[7] = 0x0B;
            Byte[] m = strTobyte(key);
            for (int j = 0; j < 6; j++)
            {
                send[i] = m[j];
                i++;
            }
            for (i = 14; i <= 29; i++)
            {
                send[i] = 0x00;
            }

            for (i = 2; i < 30; i++)
            {
                eh ^= send[i];
            }
            send[i++] = Convert.ToByte(eh);
            return send;
        }
        public static Byte[] showMoney(string key)
        {
            int i = 8;
            int eh = 0;
            Byte[] send = new Byte[200];
            send[0] = 0xAB;
            send[1] = 0xBA;
            send[2] = 0x00;//指定扇区数据命令
            send[3] = 0x12;
            send[4] = 0x09;//指定长度
            send[5] = 0x00;//扇区
            send[6] = 0x01;//块区
            send[7] = 0x0B;//密钥
            Byte[] m = strTobyte(key);
            for (int j = 0; j < 6; j++)
            {
                send[i] = m[j];
                i++;
            }
            for (i = 2; i < 14; i++)
            {
                eh ^= send[i];
            }
            send[i++] = Convert.ToByte(eh);
            return send;
        }
        public static Byte[] strTobyte(string text)
        {
            string[] str = text.Split(' ');
            byte[] b = null;
            try
            {
                b = new byte[str.Length];
                for (int i = 0; i < str.Length; i++)
                {
                    b[i] = Convert.ToByte(Convert.ToInt32(str[i], 16));
                }
                return b;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return b;
            }
        }
        //返回卡号字符串的函数
        public static string getCardIdStr()
        {
            string str = "";

            return str;
        }
    }
}

